﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;

namespace WebApplication1
{
    public partial class Caterer : System.Web.UI.Page
    {

        double total = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterScript();
            if (!IsPostBack)
            {
                //Bind site to drop down
                SiteManagement sitemgm = new SiteManagement();
                List<Site> sites = sitemgm.GetAllSite();
                foreach (Site siteobj in sites)
                {
                    ListItem li = new ListItem();
                    li.Text = siteobj.name;
                    li.Value = siteobj.id.ToString();
                    dropdownSite.Items.Add(li);
                }

                ShowCart();
            }
       
          
        }

        public void RegisterScript()
        {
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "Action", @" 
                             $(document).ready()
                {
                    var div = $(""h2:contains('0')"").parent();
                    div.removeClass(""corner"");
                    div.addClass(""Nocorner"")
                    div.find(""h2"").remove();


                }", true);

        }



        public void ShowCart()
        {

            lsvCart.DataSource = (List<Cart>)Session["orders"];
            lsvCart.DataBind();
        }

        protected void dropdownSite_SelectedIndexChanged(object sender, EventArgs e)
        {
            long siteId = Convert.ToInt64(dropdownSite.SelectedItem.Value);
            Business.Caterer cat = new Business.Caterer();
            dropdownCaterer.Items.Clear();
            dropdownCaterer.Items.Add(new ListItem("--Select Cafe--", "0"));
            dropdownCaterer.DataSource =   cat.GetCatererBySite(siteId);
            dropdownCaterer.DataTextField = "Name";
            dropdownCaterer.DataValueField = "Email";
       
            dropdownCaterer.DataBind();
            if (dropdownCaterer.Items.Count > 1)
                dropdownCaterer.Items.Insert(1,new ListItem("--ALL--", "-1"));
         

        }
        public string GetImage(object img)
        {
            byte[] image = (byte[])img;

            byte[] newBytes = Common.convertImageSize(image);


            return "data:image/jpg;base64," + Convert.ToBase64String(newBytes);
           
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Business.Food fd = new Business.Food();
            Repeater1.DataSource= fd.GetFoodBy(dropdownCaterer.SelectedValue,dropdownSite.SelectedValue);
            Repeater1.DataBind();
        }

        protected void btnAddItem_Click(object sender, EventArgs e)
        {
          
            
        }


        public void ListCart()
        {



        }
        /// <summary>
        /// Add order
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
         
            long id = Convert.ToInt64(e.CommandArgument.ToString());
            Business.Food fd = new Business.Food();
            fos_Food food = fd.GetFoodById(id);


            Cart newOrder = new Cart();
            newOrder.FoodId = id;
            newOrder.name = food.FoodName;
            if (food.isPromotion == 1)
            {
                newOrder.price = (decimal)food.PromotionPrice;
            }
            else
            {
                newOrder.price = (decimal)food.FoodPrice;
            }

            // newOrder.quantity = Convert.ToInt32();
            TextBox txtQuantity = (TextBox)e.Item.FindControl("txtQuantity");
            newOrder.quantity = Convert.ToInt32(txtQuantity.Text);
            TextBox txtCondiment = (TextBox)e.Item.FindControl("txtCondiment");
            newOrder.condiment = txtCondiment.Text;
            if ((List<Cart>)Session["orders"]!= null)
            {

               List<Cart> orders = (List<Cart>)Session["orders"];
               Cart item = orders.SingleOrDefault(c => c.FoodId == id);
               if (item !=null)
               {
                   orders.Remove(item);
                   newOrder.quantity = newOrder.quantity + item.quantity;
               }
             

               orders.Add(newOrder);
               Session["orders"] = orders;
               ShowCart();
            }
            else
            {
                List<Cart> orders = new List<Cart>();
                orders.Add(newOrder);
                Session["orders"] = orders;
                ShowCart();

            }
          
            
            

            
        }


      

        protected void lsvCart_DataBound(object sender, ListViewItemEventArgs e)
        {

            if (e.Item.ItemType == ListViewItemType.DataItem)
            {

                double price = (double)((Business.Cart)e.Item.DataItem).price;
                double quantity = (double)((Business.Cart)e.Item.DataItem).quantity;

                total = total + Convert.ToDouble(price) * Convert.ToDouble(quantity);

            }
        }

        protected void lsvCart_PreRender(object sender, EventArgs e)
        {
            Label lblTot = this.lsvCart.FindControl("lblTotal") as Label;
            if (lblTot != null)
            {

                lblTot.Text = total.ToString(); ;

            }

            
        }

        /// <summary>
        /// Remove order item
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lsvCart_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            long FoodId = Convert.ToInt64(e.CommandArgument.ToString());
            List<Cart> orders = (List<Cart>)Session["orders"];
            var remove = orders.SingleOrDefault(c=>c.FoodId == FoodId);
            orders.Remove(remove);
            ShowCart();

        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            Session["orders"] = null;
            ShowCart();
        }
    }
}